home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-06-28 | 7.4 KB | 374 lines | [TEXT/CWIE] |
- // Rectangle.cp
-
- #ifndef Rectangle_h
- #include "Rectangle.h"
- #endif
- #ifndef MinMax_h
- #include "MinMax.h"
- #endif
-
- const Rectangle Rectangle::big( minint16, minint16, maxint16, maxint16 );
- const Rectangle Rectangle::zero( 0, 0, 0, 0 );
-
- Rectangle::Rectangle( Point p1, Point p2 )
- {
- if ( p1.h <= p2.h )
- {
- left = p1.h;
- right = p2.h;
- }
- else
- {
- left = p2.h;
- right = p1.h;
- }
-
- if ( p1.v <= p2.v )
- {
- top = p1.v;
- bottom = p2.v;
- }
- else
- {
- top = p2.v;
- bottom = p1.v;
- }
- }
-
- bool Rectangle::Contains( Point p ) const
- {
- return left <= p.h && p.h < right
- && top <= p.v && p.v < bottom;
- }
-
- bool Rectangle::StrictlyContains( Point p ) const
- {
- return left < p.h && p.h < right
- && top < p.v && p.v < bottom;
- }
-
- bool Rectangle::WeaklyContains( Point p ) const
- {
- return left <= p.h && p.h <= right
- && top <= p.v && p.v <= bottom;
- }
-
- bool Rectangle::operator==( Rect r ) const
- {
- return left == r.left && right == r.right
- && top == r.top && bottom == r.bottom;
- }
-
- bool Rectangle::operator<=( Rect r ) const
- {
- return left >= r.left && right <= r.right
- && top >= r.top && bottom <= r.bottom;
- }
-
- bool Rectangle::operator>=( Rect r ) const
- {
- return left <= r.left && right >= r.right
- && top <= r.top && bottom >= r.bottom;
- }
-
- void Rectangle::operator&=( Rect r )
- {
- left = Max( left, r.left );
- top = Max( top, r.top );
- right = Min( right, r.right );
- bottom = Min( bottom, r.bottom );
-
- if ( IsEmpty() )
- *this = Rectangle( left, top, left, top );
- }
-
- void Rectangle::operator|=( Rect r )
- {
- left = Min( left, r.left );
- top = Min( top, r.top );
- right = Max( right, r.right );
- bottom = Max( bottom, r.bottom );
- }
-
- Rectangle Rectangle::operator&( Rect r ) const
- {
- Rectangle result( *this );
- result &= r;
- return result;
- }
-
- Rectangle Rectangle::operator|( Rect r ) const
- {
- Rectangle result( *this );
- result |= r;
- return result;
- }
-
- bool Rectangle::Intersects( Rect r ) const
- {
- if ( left >= r.right || r.left >= right )
- return false;
-
- if ( top >= r.bottom || r.top >= bottom )
- return false;
-
- return true;
- }
-
- bool Rectangle::Touches( Rect r ) const
- {
- if ( left > r.right || r.left > right )
- return false;
-
- if ( top > r.bottom || r.top > bottom )
- return false;
-
- return true;
- }
-
- uint32 Rectangle::SharedSides( Rect r ) const
- {
- return ( ( left == r.left ) ? 1 : 0 )
- + ( ( top == r.top ) ? 1 : 0 )
- + ( ( right == r.right ) ? 1 : 0 )
- + ( ( bottom == r.bottom ) ? 1 : 0 );
- }
-
- void Rectangle::operator+=( Rect r )
- {
- Assert( SharedSides( r ) == 3 );
- Assert( !Intersects( r ) );
- *this |= r;
- }
-
- void Rectangle::operator-=( Rect r )
- {
- Assert( SharedSides( r ) == 3 );
- Assert( *this > r );
- if ( left < r.left )
- right = r.left;
- if ( top < r.top )
- bottom = r.top;
- if ( right > r.right )
- left = r.right;
- if ( bottom > r.bottom )
- top = r.bottom;
- }
-
- Rectangle Rectangle::operator+( Rect r ) const
- {
- Rectangle result( *this );
- result += r;
- return result;
- }
-
- Rectangle Rectangle::operator-( Rect r ) const
- {
- Rectangle result( *this );
- result -= r;
- return result;
- }
-
- void Rectangle::Inset( int16 amount )
- {
- Assert( CanAdd( top, amount ) );
- Assert( CanAdd( left, amount ) );
- Assert( CanSubtract( bottom, amount ) );
- Assert( CanSubtract( right, amount ) );
-
- Assert( top + amount <= bottom - amount );
- Assert( left + amount <= right - amount );
-
- top += amount;
- left += amount;
- bottom -= amount;
- right -= amount;
- }
-
- void Rectangle::Inset( Point amount )
- {
- Assert( CanAdd( top, amount.v ) );
- Assert( CanAdd( left, amount.h ) );
- Assert( CanSubtract( bottom, amount.v ) );
- Assert( CanSubtract( right, amount.h ) );
-
- Assert( top + amount.v <= bottom - amount.v );
- Assert( left + amount.h <= right - amount.h );
-
- top += amount.v;
- left += amount.h;
- bottom -= amount.v;
- right -= amount.h;
- }
-
- void Rectangle::Inset( Rect amount )
- {
- Assert( CanSubtract( top, amount.top ) );
- Assert( CanSubtract( left, amount.left ) );
- Assert( CanSubtract( bottom, amount.bottom ) );
- Assert( CanSubtract( right, amount.right ) );
-
- Assert( top - amount.top <= bottom - amount.bottom );
- Assert( left - amount.left <= right - amount.right );
-
- top -= amount.top;
- left -= amount.left;
- bottom -= amount.bottom;
- right -= amount.right;
- }
-
- void Rectangle::Outset( int16 amount )
- {
- Assert( CanSubtract( top, amount ) );
- Assert( CanSubtract( left, amount ) );
- Assert( CanAdd( bottom, amount ) );
- Assert( CanAdd( right, amount ) );
-
- Assert( top - amount <= bottom + amount );
- Assert( left - amount <= right + amount );
-
- top -= amount;
- left -= amount;
- bottom += amount;
- right += amount;
- }
-
- void Rectangle::Outset( Point amount )
- {
- Assert( CanSubtract( top, amount.v ) );
- Assert( CanSubtract( left, amount.h ) );
- Assert( CanAdd( bottom, amount.v ) );
- Assert( CanAdd( right, amount.h ) );
-
- Assert( top - amount.v <= bottom + amount.v );
- Assert( left - amount.h <= right + amount.h );
-
- top -= amount.v;
- left -= amount.h;
- bottom += amount.v;
- right += amount.h;
- }
-
- void Rectangle::Outset( Rect amount )
- {
- Assert( CanAdd( top, amount.top ) );
- Assert( CanAdd( left, amount.left ) );
- Assert( CanAdd( bottom, amount.bottom ) );
- Assert( CanAdd( right, amount.right ) );
-
- Assert( top + amount.top <= bottom + amount.bottom );
- Assert( left + amount.left <= right + amount.right );
-
- top += amount.top;
- left += amount.left;
- bottom += amount.bottom;
- right += amount.right;
- }
-
- void Rectangle::operator+=( Point vector )
- {
- Assert( CanAdd( top, vector.v ) );
- Assert( CanAdd( left, vector.h ) );
- Assert( CanAdd( bottom, vector.v ) );
- Assert( CanAdd( right, vector.h ) );
- top += vector.v;
- left += vector.h;
- bottom += vector.v;
- right += vector.h;
- }
-
- void Rectangle::operator-=( Point vector )
- {
- Assert( CanSubtract( top, vector.v ) );
- Assert( CanSubtract( left, vector.h ) );
- Assert( CanSubtract( bottom, vector.v ) );
- Assert( CanSubtract( right, vector.h ) );
- top -= vector.v;
- left -= vector.h;
- bottom -= vector.v;
- right -= vector.h;
- }
-
- Rectangle Rectangle::operator+( Point vector ) const
- {
- Rectangle result( *this );
- result += vector;
- return result;
- }
-
- Rectangle Rectangle::operator-( Point vector ) const
- {
- Rectangle result( *this );
- result -= vector;
- return result;
- }
-
- PointObject Rectangle::Size() const
- {
- Assert( Height() <= maxint16 );
- Assert( Width() <= maxint16 );
- return PointObject( Width(), Height() );
- }
-
- uint32 Rectangle::Bulk() const
- {
- return Height() + Width();
- }
-
- uint32 Rectangle::Area() const
- {
- return Height() * Width();
- }
-
- uint32 Rectangle::TaxicabDistanceTo( Point p ) const
- {
- uint32 distance = 0;
-
- if ( p.h < left )
- distance += int32( left ) - int32( p.h );
- else
- if ( p.h > right )
- distance += int32( p.h ) - int32( right );
-
- if ( p.v < top )
- distance += int32( top ) - int32( p.v );
- else
- if ( p.v > bottom )
- distance += int32( p.v ) - int32( bottom );
-
- return distance;
- }
-
- uint32 Rectangle::TaxicabDistanceTo( Rect r ) const
- {
- uint32 distance = 0;
-
- if ( r.right < left )
- distance += int32( left ) - int32( r.right );
- else
- if ( r.left > right )
- distance += int32( r.left ) - int32( right );
-
- if ( r.bottom < top )
- distance += int32( top ) - int32( r.bottom );
- else
- if ( r.top > bottom )
- distance += int32( r.top ) - int32( bottom );
-
- return distance;
- }
-
- void Rectangle::GlobalToLocal()
- {
- PointObject offset( 0, 0 );
- offset.GlobalToLocal();
- *this += offset;
- }
-
- void Rectangle::LocalToGlobal()
- {
- PointObject offset( 0, 0 );
- offset.LocalToGlobal();
- *this += offset;
- }
-